//201902104003邵潇漩
package bysj.dao;
import bysj.domain.Degree;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;

public final class DegreeDao {
    private static DegreeDao degreeDao =
            new DegreeDao();
    private static Collection<Degree> degrees;

  /*  static {
        degrees = new TreeSet<Degree>();
        Degree doctor = new Degree(1,
                "博士", "01", "");
        degrees.add(doctor);
        degrees.add(new Degree(2,
                "硕士", "02", ""));
        degrees.add(new Degree(3,
                "学士", "03", ""));
    }*/

    private DegreeDao() {
    }

    public static DegreeDao getInstance() {
        return degreeDao;
    }

    public static Collection<Degree> findAll(Connection connection , List<Condition> conditionList, Pagination pagination) throws SQLException {
        Collection<Degree> degrees = new TreeSet<Degree>();
        int totalNum = CommonDao.getInstance().count(connection,"degree");
        //创建查询的主语句
        StringBuilder sql_select = new StringBuilder("SELECT * FROM degree");
        //如果有条件，则生成条件子句，并附加到主语句后面
        if(conditionList!=null){
            String whereClause = Condition.toWhereClause(conditionList);
            sql_select.append(whereClause);
        }
        if(pagination!=null){
            String LimitClause = pagination.toLimitClause(totalNum);
            sql_select.append(LimitClause);
        }
        //获得查询的预编译语句对象
        PreparedStatement pstmt = connection.prepareStatement(sql_select.toString());
        //执行sql查询语句并获得结果集对象
        ResultSet resultSet = pstmt.executeQuery();
        //遍历结果集，根据元组创建对象，并添加到集合中
        while(resultSet.next()){
            degrees.add(new Degree(resultSet.getInt("id"),
                    resultSet.getString("description"),
                    resultSet.getString("no"),
                    resultSet.getString("remarks")
            ));
        }
        JdbcHelper.close(resultSet,pstmt);
        return degrees;
    }

    public Degree find(Connection conn,Integer id) throws SQLException {
        Degree desiredDegree = null;
        String sql_select_id="SELECT*FROM degree WHERE id=?";
        PreparedStatement pstmt = conn.prepareStatement(sql_select_id);
        pstmt.setInt(1,id);
        ResultSet resultSet=pstmt.executeQuery();
        if(resultSet.next()){
            String description=resultSet.getString("description");
            String no = resultSet.getString("no");
            String remarks = resultSet.getString("remarks");
            desiredDegree=new Degree(id,description,no,remarks);
        }
        JdbcHelper.close(resultSet,pstmt);
        return desiredDegree;
    }

    public boolean update(Connection conn,Degree degree) throws SQLException {
        //创建sql语句
        String updateDegree_sql = "UPDATE degree SET description=?, no=?, remarks=? WHERE id=?";
        //在该连接上创建预编译语句对象
        PreparedStatement pstmt = conn.prepareStatement(updateDegree_sql);
        //为预编译参数赋值
        pstmt.setString(1,degree.getDescription());
        pstmt.setString(2,degree.getNo());
        pstmt.setString(3,degree.getRemarks());
        pstmt.setInt(4,degree.getId());
        //执行预编译语句的executeUpdate方法,获得修改的记录行数
        int affectedRowNum=pstmt.executeUpdate();
        //关闭连接
        JdbcHelper.close(null,pstmt);
        //返回school对象是否修改成功
        return affectedRowNum>0;
    }

    public boolean add(Connection conn,Degree degree) throws SQLException {
        String addDegree_sql="INSERT INTO degree "+"(description,no,remarks)value(?,?,?)";
        //在该连接上创建预编译语句对象
        PreparedStatement pstmt = conn.prepareStatement(addDegree_sql);
        //为预编译参数赋值
        pstmt.setString(1, degree.getDescription());
        pstmt.setString(2, degree.getNo());
        pstmt.setString(3, degree.getRemarks());
        int rowAffected =pstmt.executeUpdate();
        JdbcHelper.close(null,pstmt);
        return rowAffected>0;
    }

    public boolean delete(Connection connection,Integer id) throws SQLException {
        //创建sql语句
        String deleteDegree_sql = "DELETE FROM degree WHERE id=?";
        //在该连接上创建预编译语句对象
        PreparedStatement pstmt = connection.prepareStatement(deleteDegree_sql);
        //为预编译参数赋值
        pstmt.setInt(1, id);
        //执行预编译语句的executeUpdate方法,获得删除的记录行数
        int affectedRowNum=pstmt.executeUpdate();
        //关闭连接
        JdbcHelper.close(null,pstmt);
        //如果删除记录的行数不为0，返回true,否则返回false
        return affectedRowNum>0;
    }

    public boolean delete(Connection connection,Degree degree) throws SQLException {
        return this.delete(connection,degree.getId());
    }
}

